* ======================================
* ---------- Coworkers Split -----------
* ======================================


	use $dta\EstimationSample.dta, clear


* Split by size
* =============

	keep if Work1 == 1
	bysort Work1net: gen Work1_size = (_N/2)+1 if Work1 == 1
	bysort Work2net: gen Work2_size = (_N/2)+1 if Work2 == 1
	* Divide by two to get one period, plus one to count self

	* Size bins
	foreach net in Work1 Work2{
		gen `net'_s1 = 1*(`net'_size <= 5)	
		gen `net'_s2 = 1*(`net'_size >= 6 & `net'_size <= 15)
		gen `net'_s3 = 1*(`net'_size >= 16 & `net'_size <= .)
	
	* Estimation variables
	forvalues s = 1/3{
		gen `net'Any_s`s' = 1*(`net'Any == 1 & `net'_s`s' == 1)
		gen `net'Same_s`s' = 1*(`net'Same == 1 & `net'_s`s' == 1)
		}
	}

	
* Estimate models
* ===============

	egen idbsu = group(Voter_id Voter_bsu)
	gen period = 1*(year == 2019)
	xtset idbsu period
	estimates clear


	forvalues s = 1/3{
		foreach net in Work1 Work2{
	
		preserve
		keep if `net'_s`s' == 1 // Split by n of co-workers
		
		sum Voter_turnout // mean depvar
		local meandepvar = string(r(mean)*100, "%15.2fc")
	
		gen Any = `net'Any_s`s'
		gen Same = `net'Same_s`s'
		
		* Regression
		eststo m_`net'_`s': xtreg Voter_turnout Any Same i.period, ///
			i(idbsu) fe cluster(Voter_bsu)
		quietly estadd local meandep `meandepvar', replace
		
		restore
		}
	}


* Export results
* ==============

	esttab m_* ///
		using $tbls\SplitCoworkers.tex, replace  style(tex) ///
		prehead("\begin{tabular}{l*{7}{c}} \hline\hline \\ ") ///
		posthead("\hline \\") ///
		fragment ///
		mtitles("Age-estbl." "Estbl." "Age-estbl." "Estbl." "Age-estbl." ///
			"Estbl.") ///
		mgroups("2-5 co-workers" "6-15 co-workers" "16+ co-workers", ///
			pattern(1 0 1 0 1 0) span prefix(\multicolumn{@span}{c}{) suffix(}) ///
			end(\cmidrule(lr){2-3} \cmidrule(lr){4-5} \cmidrule(lr){6-7})) ///
		label se b(%9.3f) se(%9.3f) nostar ///
		refcat(Any "\shortstack{ No network candidate }") ///
		coeflabels(Any "Any District" Same "Same District" ) ///
		drop(_cons *.period ) noomitted nobaselevels ///
		nonotes stats(N N_clust meandep, labels( ///
			"Observations" "Clusters" "Mean turnout (\%)" ) ///
			fmt(%15.0fc %15.0fc %15.0fc)) substitute(\_ _) ///
		prefoot("\hline") ///
		postfoot("\hline\hline \\ \end{tabular}")
		